home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Caml Light 0.61 / Source / src / runtime / io.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-24  |  1.9 KB  |  64 lines  |  [TEXT/MPS ]

  1. /* Buffered input/output */
  2.  
  3. #ifndef _io_
  4. #define _io_
  5.  
  6.  
  7. #include "mlvalues.h"
  8.  
  9. #ifndef IO_BUFFER_SIZE
  10. #define IO_BUFFER_SIZE 4096
  11. #endif
  12.  
  13. struct channel {
  14.   int fd;                       /* Unix file descriptor */
  15.   long offset;                  /* Absolute position of fd in the file */
  16.   char * curr;                  /* Current position in the buffer */
  17.   char * max;                   /* Logical end of the buffer */
  18.   char * end;                   /* Physical end of the buffer */
  19.   char buff[IO_BUFFER_SIZE];    /* The buffer itself */
  20. };
  21.  
  22. /* For an output channel:
  23.      [offset] is the absolute position of the beginning of the buffer [buff].
  24.    For an input channel:
  25.      [offset] is the absolute position of the logical end of the buffer [max].
  26. */
  27.  
  28. #define putch(channel, ch)                                                    \
  29.   { if ((channel)->curr >= (channel)->end) flush(channel);                    \
  30.     *((channel)->curr)++ = (ch);                                              \
  31.     if ((channel)->curr > (channel)->max) (channel)->max = (channel)->curr; }
  32.  
  33. #define getch(channel)                                                        \
  34.   ((channel)->curr >= (channel)->max                                          \
  35.    ? refill(channel)                                                          \
  36.    : (unsigned char) *((channel))->curr++)
  37.  
  38. #ifdef ANSI
  39.  
  40. extern struct channel * open_descriptor(value);
  41. extern value flush(struct channel *);
  42. extern void putword(struct channel *, long);
  43. extern void putblock(struct channel *, char *, unsigned);
  44. extern unsigned char refill(struct channel *);
  45. extern long getword(struct channel *);
  46. extern unsigned getblock(struct channel *, char *, unsigned);
  47. extern value close_in (struct channel *);
  48.  
  49. #else
  50.  
  51. struct channel * open_descriptor();
  52. value flush();
  53. void putword();
  54. void putblock();
  55. unsigned char refill();
  56. long getword();
  57. unsigned getblock();
  58. value close_in ();
  59.  
  60. #endif
  61.  
  62.  
  63. #endif /* _io_ */
  64.